package com.kwlt.smc.rt;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springside.modules.test.spring.SpringTransactionalTestCase;

import com.kwlt.smc.dao.RptSendDetailDao;
import com.kwlt.smc.dao.UserDao;
import com.kwlt.smc.entity.SmsSendDetail;
import com.kwlt.smc.entity.User;
import com.kwlt.smc.util.ConUtils;


@ContextConfiguration(locations = { "/applicationContext.xml" })
public class UpdateLocalRpt extends SpringTransactionalTestCase {
	private static final int UPDATE_SIZE = 1000;
	@Autowired
	private UserDao userDao;
	@Autowired
	private RptSendDetailDao rptSendDetailDao;
	
	@Test
	@Rollback(false)
	public void updateRpt(){
		String path = "D:\\rpt\\samprpt.20120923.txt";
		long begin = System.currentTimeMillis();
		File file=new File(path);
		long count=updateSmsSendDetail(file);
		long end = System.currentTimeMillis();
		System.out.println("总共补全数据"+count+"条");
		System.out.println("总执行时间："+(end-begin)/(60*1000));
	}
	
	public long updateSmsSendDetail(File moFile) {
		int i=0;
		try {
			List<SmsSendDetail> list = new ArrayList<SmsSendDetail>();
			LineIterator li = FileUtils.lineIterator(moFile);
			while (li.hasNext()) {
				String str = li.next();
				if (!StringUtils.isBlank(str)) {
					i++;
					updateSmsSendDetail(str, list);
				}
			}
			if(list.size()>0){
				String localMsgId=list.get(0).getLocalMsgId();
				String dateStr= "201" + localMsgId.substring(4, 9);
				rptSendDetailDao.save(list, dateStr);
				System.out.println("更新了"+dateStr+"剩余的"+list.size()+"条");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	private void updateSmsSendDetail(String str, List<SmsSendDetail> list) {
		String[] strArr = str.split(" ");
		String phoneNumber = strArr[0];
		String localMsgId = strArr[1];
		if(!localMsgId.startsWith("00")){
			//System.out.println("phoneNumber:"+phoneNumber+"没有返回localMsgId，不能更新!");
			return;
		}
		String dateStr = "201" + localMsgId.substring(4, 9);
		boolean bool = rptSendDetailDao.getRptSendDetailByLocalMsgId(localMsgId, phoneNumber, dateStr);
		SmsSendDetail smsSendDetail=null;
		if (bool) {
			Date sendDate = ConUtils.revDate("201" + localMsgId.substring(4, 15), "yyyyMMddHHmmss");
			String sendDateStr = ConUtils.formatDate(sendDate,"yyyy-MM-dd HH:mm:ss");
			String operBakMsgId = strArr[3];
			String rptValue ="";
			String rptFlag ="1";
			if(!StringUtils.isBlank(strArr[4]) && strArr[4].length()>7){
				rptValue= strArr[4].substring(0, 7);
				rptFlag= strArr[4].substring(0, 7).equals("DELIVRD") ? "0": "1";
			}
			User u = userDao.findByUserNumbers(localMsgId.substring(0, 4));
			String sendSuccessFlag = "0";
			String user = u.getLoginName();
			Date intSubmitDate = sendDate;
			Date rptDate = sendDate;
			String rptDateStr = sendDateStr;
			String channel = "sh000";
			String operators = "mobile";
			String userDate = ConUtils.formatDate(intSubmitDate, "yyyy-MM-dd");
			
			smsSendDetail=this.getSmsSendDetail(phoneNumber, sendDate, sendDateStr,
					sendSuccessFlag, rptFlag, rptValue, user, rptDate,
					rptDateStr, userDate, channel, intSubmitDate, localMsgId,
					operBakMsgId, operators);
			if(list.size()>0){
				SmsSendDetail ssd=list.get(list.size()-1);
				String lastDateStr = "201" + ssd.getLocalMsgId().substring(4, 9);
				if(!dateStr.equals(lastDateStr)){//如果不是更新同一天的记录
					rptSendDetailDao.save(list, lastDateStr);//批次更新前一天记录
					System.out.println("更新了"+lastDateStr+"的"+list.size()+"条");
					list.clear();
					list.add(smsSendDetail);
				}else{
					list.add(smsSendDetail);
				}
				if(list.size()>UPDATE_SIZE){
					rptSendDetailDao.save(list, dateStr);//批次更新记录
					System.out.println("更新了"+dateStr+"的"+UPDATE_SIZE+"条");
					list.clear();
				}
			}else{
				list.add(smsSendDetail);
			}
		}
	}

	private SmsSendDetail getSmsSendDetail(String phoneNumber, Date sendDate,
			String sendDateStr, String sendSuccessFlag, String rptFlag,
			String rptValue, String user, Date rptDate, String rptDateStr,
			String userDate, String channel, Date intSubmitDate,
			String localMsgId, String operBakMsgId, String operators) {
		SmsSendDetail smsSendDetail = new SmsSendDetail();
		smsSendDetail.setPhoneNum(phoneNumber);
		smsSendDetail.setSendDate(sendDate);
		smsSendDetail.setSendDateStr(sendDateStr);
		smsSendDetail.setSendSuccessFlag(sendSuccessFlag);
		smsSendDetail.setRptFlag(rptFlag);
		smsSendDetail.setRptValue(rptValue);
		smsSendDetail.setUser(user);
		smsSendDetail.setRptDate(rptDate);
		smsSendDetail.setRptDateStr(rptDateStr);
		smsSendDetail.setUserDate(userDate);
		smsSendDetail.setChannel(channel);
		smsSendDetail.setIntSubmitDate(intSubmitDate);
		smsSendDetail.setLocalMsgId(localMsgId);
		smsSendDetail.setOperBakMsgId(operBakMsgId);
		smsSendDetail.setOperators(operators);
		return smsSendDetail;
	}
}
